package com.wcar.vipmanagement.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcar.common.entity.*;
import com.wcar.common.util.WebUtil;
import com.wcar.common.vo.Result;
import com.wcar.vipmanagement.dao.*;
import com.wcar.vipmanagement.service.FrontUserService;
import com.wcar.vipmanagement.vo.LoginVo;
import com.wcar.vipmanagement.vo.UserVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
* @author 21147
* @description 针对表【tb_front_user】的数据库操作Service实现
* @createDate 2024-05-24 15:05:35
*/
@Service
public class FrontUserServiceImpl implements FrontUserService {
    @Resource
    private FrontUserMapper frontUserMapper;
    @Resource
    private StoreUserDao storeUserDao;
    @Autowired
    private UserAddressDao userAddressDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public Result<Page<FrontUser>> getCustomer(UserVo userVo) {
        //获取token
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("user:login:" + token);
        User user = JSON.parseObject(s, User.class);
        QueryWrapper<StoreUser> wrapper = new QueryWrapper<>();
        if(user.getUserId()!=1){
            wrapper.eq("tfu.user_id",user.getUserId());
        }
        List<StoreUser> storeUsers = storeUserDao.selectList(wrapper);
        ArrayList<Integer> userIds = new ArrayList<>();
        for (StoreUser storeUser : storeUsers) {
            userIds.add(storeUser.getStoreId());
        }
        Page<FrontUser> page=new Page<>(userVo.getCurrentPage(),userVo.getPageSize());
        QueryWrapper<FrontUser> query = new QueryWrapper<>();
        if(userVo.getUserName()!=null&&userVo.getUserName()!=""){
            query.like("user_name",userVo.getUserName());
        }
        if(userIds.size()==0){
            return new Result<>(201,"获取用户信息成功",null);
        }
        wrapper.in("tfus.store_id",userIds);
        List<LoginVo> loginVos = frontUserMapper.selectMaxInfoId(wrapper);
        ArrayList<Integer> infoIds = new ArrayList<>();
        for (LoginVo loginVo : loginVos) {
            infoIds.add(loginVo.getInfoId());
        }
        if(userIds.size()==0||infoIds.size()==0){
            return new Result<>(201,"获取用户信息成功",null);
        }
        query.in("tfus.store_id",userIds);
        query.in("tll.info_id",infoIds);
        Page<FrontUser> list = frontUserMapper.getPage(page, query);
        return new Result<>(200,"获取用户信息成功",list);
    }

    @Override
    public Result<FrontUser> getallByid() {
        //拿到角色id
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("font:login:" + token);
        FrontUser frontUser = JSON.parseObject(s, FrontUser.class);
        return new Result<>(200,"获取用户信息成功",frontUser);
    }

    @Override
    public Result<List<UserAddress>> selectAddressById(Integer userId) {
        QueryWrapper<UserAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<UserAddress> list = userAddressDao.selectList(wrapper);
        return new Result<>(200,"根据id获取地址成功",list);
    }

}




